package com.ys.peony.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.IdRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.ys.peony.app.AppManager;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.enums.SwipeDirect;
import com.ys.peony.utils.AnimUtils;
import com.ys.peony.utils.AppUtils;
import com.ys.peony.utils.FileUtils;

import butterknife.ButterKnife;

/**
 * Activity基类
 *
 * @author Hunter
 */
public abstract class BaseActivity extends AppCompatActivity {

    public final static int RESPONSE_CODE = 0;
    //当前页数
    protected int currentPage = 0;

    //每页显示条目数
    protected int pageNum = 10;

    private Toast toast;

    private ProgressDialog mProgressDialog;
    public Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindow();
        setContentView(getContentResId());
        //1.初始化View注入
        ButterKnife.bind(this);
        //2.设置Activity
        AppUtils.setActivity(this);
        //3.添加activity到堆栈中
        AppManager.getAppManager().addActivity(this);
        //4.禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
*/
        //5.初始化View
        initView();
    }


    protected void initWindow() {
    }

    protected abstract int getContentResId();

    /**
     * 初始化控件
     */
    protected abstract void initView();


    @Override
    public void finish() {
        super.finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //hideInputWindow();
    }

    public void showProgress(boolean flag, String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(flag);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }

    public void showProgress(String message) {
        showProgress(true, message);
    }

    public void setProgressCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        if (mProgressDialog != null) {
            mProgressDialog.setOnCancelListener(onCancelListener);
        }
    }

    public void hideProgress() {
        if (mProgressDialog == null) {
            return;
        }
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    public void showToast(String msg) {
        if (!isFinishing()) {
            if (toast == null) {
                toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }
    }

    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle) {
        skipToTargetPage(clazz, bundle, SwipeDirect.DIRECT_LEFT);
    }

    /**
     * 跳转到对应的页面
     *
     * @param clazz
     * @param bundle
     * @param direct
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct) {
        skipToTargetPage(clazz, bundle, direct, false);
    }

    /**
     * @param clazz
     * @param bundle
     * @param direct
     * @param isResult ture:需要返回，false：不需要返回
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct, Boolean isResult) {
        skipToTargetPage(clazz, bundle, direct, isResult, RESPONSE_CODE);
    }

    /**
     * @param clazz
     * @param bundle
     * @param direct
     * @param isResult ture:需要返回，false：不需要返回
     */
    public void skipToTargetPage(Class<? extends Activity> clazz, Bundle bundle, SwipeDirect direct, Boolean isResult, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(BaseApplication.getInstance(), clazz);
        if (bundle == null) {
            bundle = new Bundle();
        }
        intent.putExtras(bundle);
        if (isResult) {
            this.startActivityForResult(intent, requestCode);
        } else {
            this.startActivity(intent);
        }
        switch (direct) {
            case DIRECT_UP:
                AnimUtils.bottomInTranslateAnim();
                break;
            case DIRECT_DOWN:
                AnimUtils.upInTranslateAnim();
                break;
            case DIRECT_LEFT:
                AnimUtils.inAnim();
                break;
            case DIRECT_RIGHT:
                AnimUtils.outAnim();
                break;
            case DIRECT_FADE:
                AnimUtils.fadeAlphaAnim();
                break;
            default:
                AnimUtils.inAnim();
                break;
        }
    }

    public void hideInput() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive() && getCurrentFocus() != null) {
                    if (getCurrentFocus().getWindowToken() != null) {
                        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public <T extends View> T $(@IdRes int resId){
        return (T)super.findViewById(resId);
    }

}
